package com.yesep.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

/**
 * @program: envm
 * @description: 时间工具类
 * @author: quliang
 * @create: 2019-07-17 15:38
 **/
public class DateUtils {
    public static final String DATE_FORMAT = "yyyy-MM-dd";
    public static final String DATE_CHS_FORMAT = "yyyy年MM月dd日";
    public static final String TIME_FORMAT = "HH:mm:ss";
    public static final String STIME_FORMAT = "HH:mm";
    public static final String DATE_STIME_FORMAT = "yyyy-MM-dd HH:mm";
    public static final String DATE_TIME_FORMAT = "yyyy-MM-dd HH:mm:ss";
    public static final String TIMESTAMP_FORMAT = "yyyy-MM-dd HH:mm:ss.SSS";
    public static final String DATA_TIME_NO_SPLASH_FORMAT = "yyyyMMddHHmmss";
    public static final String TIMESTAMP_NO_SPLASH_FORMAT = "yyyyMMddHHmmssSSS";

    public DateUtils() {
    }

    public static Calendar getCalendar() {
        return Calendar.getInstance();
    }

    public static int getYear() {
        return getCalendar().get(1);
    }

    public static int getMonth() {
        return getCalendar().get(2) + 1;
    }

    public static int getDay4Month() {
        return getCalendar().get(5) + 1;
    }

    public static Calendar getCalendar(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        return cal;
    }

    public static Date getDate() {
        return new Date();
    }

    public static Date getDate(String date, String format) {
        if (date == null) {
            return new Date();
        } else {
            if (StringUtils.empty(format)) {
                format = "yyyy-MM-dd";
            }

            SimpleDateFormat sdf = new SimpleDateFormat(format);

            try {
                return sdf.parse(date);
            } catch (ParseException var4) {
                var4.printStackTrace();
                return new Date();
            }
        }
    }

    public static String getDatetimeText(Date date) {
        return getDateText(date, "yyyy-MM-dd HH:mm:ss");
    }

    public static SimpleDateFormat getDateFormat(String format) {
        if (StringUtils.empty(format)) {
            format = "yyyy-MM-dd";
        }

        SimpleDateFormat sdf = new SimpleDateFormat(format);
        return sdf;
    }

    public static String getDateText(Date date, String format) {
        if (date == null) {
            return "";
        } else {
            if (StringUtils.empty(format)) {
                format = "yyyy-MM-dd";
            }

            SimpleDateFormat sdf = new SimpleDateFormat(format);
            return sdf.format(date);
        }
    }

    public static Date timeStrToDate(String str) {
        if (str == null) {
            return null;
        } else {
            str = str.trim();
            if (str.length() != 14 && str.length() != 8) {
                return null;
            } else {
                Calendar time = Calendar.getInstance();
                time.clear();

                try {
                    String tmp = str.substring(0, 4);
                    int y = Integer.parseInt(tmp);
                    tmp = str.substring(4, 6);
                    int m = Integer.parseInt(tmp) - 1;
                    tmp = str.substring(6, 8);
                    int d = Integer.parseInt(tmp);
                    if (str.length() > 8) {
                        tmp = str.substring(8, 10);
                        int h = Integer.parseInt(tmp);
                        tmp = str.substring(10, 12);
                        int min = Integer.parseInt(tmp);
                        tmp = str.substring(12, 14);
                        int s = Integer.parseInt(tmp);
                        time.set(y, m, d, h, min, s);
                    } else {
                        time.set(y, m, d);
                    }
                } catch (NumberFormatException var9) {
                    return null;
                }

                return time.getTime();
            }
        }
    }

    public static Calendar toTime(String str) {
        if (str == null) {
            return null;
        } else if (str.length() != 14) {
            return null;
        } else {
            Calendar time = Calendar.getInstance();
            time.clear();

            try {
                String tmp = str.substring(0, 4);
                int y = Integer.parseInt(tmp);
                tmp = str.substring(4, 6);
                int m = Integer.parseInt(tmp) - 1;
                tmp = str.substring(6, 8);
                int d = Integer.parseInt(tmp);
                tmp = str.substring(8, 10);
                int h = Integer.parseInt(tmp);
                tmp = str.substring(10, 12);
                int min = Integer.parseInt(tmp);
                tmp = str.substring(12, 14);
                int s = Integer.parseInt(tmp);
                time.set(y, m, d, h, min, s);
                return time;
            } catch (NumberFormatException var9) {
                return null;
            }
        }
    }

    public static Date addDay(Date date, int day) {
        if (date == null) {
            return null;
        } else {
            Calendar c = Calendar.getInstance();
            c.setTime(date);
            c.add(5, day);
            return c.getTime();
        }
    }

    public static String formatDateString(String date) {
        return date != null && !date.trim().equals("") ? date.replaceAll("/", "-") : date;
    }

    public static Date stringToDate(String myDateStr) {
        myDateStr = formatDateString(myDateStr);
        if (myDateStr.length() < 10) {
            return null;
        } else {
            String sFormat = "yyyy-MM-dd";
            SimpleDateFormat sdf = new SimpleDateFormat(sFormat);
            Date d = null;

            try {
                d = sdf.parse(myDateStr);
                return d;
            } catch (ParseException var5) {
                return null;
            }
        }
    }

    public static Date stringToDate(String myDateStr, String format) {
        myDateStr = formatDateString(myDateStr);
        if (myDateStr.length() < 10) {
            return null;
        } else {
            if (format == null || format.trim().equals("")) {
                format = "yyyy-MM-dd";
            }

            SimpleDateFormat sdf = new SimpleDateFormat(format);
            Date d = null;

            try {
                d = sdf.parse(myDateStr);
                return d;
            } catch (ParseException var5) {
                return null;
            }
        }
    }

    public static Date stringToDatetime(String myDateStr) {
        myDateStr = formatDateString(myDateStr);
        if (myDateStr.length() < 19) {
            myDateStr = myDateStr.substring(0, 10) + " 00:00:00";
        }

        myDateStr = myDateStr.substring(0, 19);
        String sFormat = "yyyy-MM-dd HH:mm:ss";
        SimpleDateFormat sdf = new SimpleDateFormat(sFormat);
        Date d = null;

        try {
            d = sdf.parse(myDateStr);
            return d;
        } catch (ParseException var5) {
            return null;
        }
    }

    public static Date longToDatetime(long dateVal) {
        return new Date(dateVal);
    }

    public static Date longToDatetime(Long dateVal) {
        return longToDatetime(dateVal);
    }

    public static String dateToString(Date myDate) {
        if (myDate == null) {
            return null;
        } else {
            SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
            return sf.format(myDate);
        }
    }

    public static String dateToChineseString(Date myDate) {
        if (myDate == null) {
            return null;
        } else {
            SimpleDateFormat sf = new SimpleDateFormat("yyyy年MM月dd日");
            return sf.format(myDate);
        }
    }

    public static String getTimeByFormat(Date myDate, String formatString) {
        if (myDate == null) {
            return null;
        } else {
            SimpleDateFormat sf = new SimpleDateFormat(formatString);
            return sf.format(myDate);
        }
    }

    public static String datetimeToString(Date myDate) {
        if (myDate == null) {
            return null;
        } else {
            SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            return sf.format(myDate);
        }
    }

    public static String datetimeToString24(Date myDate) {
        if (myDate == null) {
            return null;
        } else {
            SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            return sf.format(myDate);
        }
    }

    public static String datetimeToString12(Date myDate) {
        if (myDate == null) {
            return null;
        } else {
            SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
            return sf.format(myDate);
        }
    }

    public static String dateToNoSplashString(Date myDate) {
        if (myDate == null) {
            return null;
        } else {
            SimpleDateFormat sf = new SimpleDateFormat("yyyyMMdd");
            return sf.format(myDate);
        }
    }

    public static String datetimeToNoSplashString(Date myDate) {
        if (myDate == null) {
            return null;
        } else {
            SimpleDateFormat sf = new SimpleDateFormat("yyyyMMddHHmmss");
            return sf.format(myDate);
        }
    }

    public static String datetimestampToNoSplashString(Date myDate) {
        if (myDate == null) {
            return null;
        } else {
            SimpleDateFormat sf = new SimpleDateFormat("yyyyMMddHHmmssSSS");
            return sf.format(myDate);
        }
    }

    public static Date addMonth(Date myDate, int difMonth) {
        Calendar cal = GregorianCalendar.getInstance();
        if (myDate == null) {
            return null;
        } else {
            cal.setTime(myDate);
            cal.add(2, difMonth);
            return cal.getTime();
        }
    }

    public static Date addYear(Date myDate, int difYear) {
        Calendar cal = GregorianCalendar.getInstance();
        if (myDate == null) {
            return null;
        } else {
            cal.setTime(myDate);
            cal.add(1, difYear);
            return cal.getTime();
        }
    }

    public static int getDaysOfMonth(Date myDate) {
        GregorianCalendar gcal = new GregorianCalendar();
        if (myDate == null) {
            return 0;
        } else {
            gcal.setTime(myDate);
            int days = gcal.getActualMaximum(5);
            return days;
        }
    }

    public static Date getFirstDateOfMonth(Date myDate) {
        GregorianCalendar gcal = new GregorianCalendar();
        if (myDate == null) {
            return gcal.getTime();
        } else {
            gcal.setTime(myDate);
            gcal.set(gcal.get(1), gcal.get(2), 1);
            return gcal.getTime();
        }
    }

    public static String getFirstDayOfMonth(Date myDate) {
        GregorianCalendar gcal = new GregorianCalendar();
        if (myDate == null) {
            return "";
        } else {
            gcal.setTime(myDate);
            SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-");
            return sf.format(gcal.getTime()) + "01";
        }
    }

    public static Date getEndDateOfMonth(Date myDate) {
        GregorianCalendar gcal = new GregorianCalendar();
        if (myDate == null) {
            return gcal.getTime();
        } else {
            gcal.setTime(myDate);
            int days = getDaysOfMonth(myDate);
            gcal.set(gcal.get(1), gcal.get(2), days);
            return gcal.getTime();
        }
    }

    public static String getEndDayOfMonth(Date myDate) {
        GregorianCalendar gcal = new GregorianCalendar();
        if (myDate == null) {
            return "";
        } else {
            gcal.setTime(myDate);
            int days = getDaysOfMonth(myDate);
            SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-");
            return sf.format(gcal.getTime()) + Integer.toString(days);
        }
    }

    public static Date getFirstDateOfYear(Date myDate) {
        GregorianCalendar gcal = new GregorianCalendar();
        if (myDate == null) {
            return gcal.getTime();
        } else {
            gcal.setTime(myDate);
            gcal.set(gcal.get(1), 0, 1);
            return gcal.getTime();
        }
    }

    public static String getFirstDayOfYear(Date myDate) {
        GregorianCalendar gcal = new GregorianCalendar();
        if (myDate == null) {
            return "";
        } else {
            gcal.setTime(myDate);
            SimpleDateFormat sf = new SimpleDateFormat("yyyy-");
            return sf.format(gcal.getTime()) + "01-01";
        }
    }

    public static Date getEndDateOfYear(Date myDate) {
        GregorianCalendar gcal = new GregorianCalendar();
        if (myDate == null) {
            return gcal.getTime();
        } else {
            gcal.setTime(myDate);
            gcal.set(gcal.get(1), 11, 31);
            return gcal.getTime();
        }
    }

    public static String getEndDayOfYear(Date myDate) {
        GregorianCalendar gcal = new GregorianCalendar();
        if (myDate == null) {
            return "";
        } else {
            gcal.setTime(myDate);
            SimpleDateFormat sf = new SimpleDateFormat("yyyy-");
            return sf.format(gcal.getTime()) + "12-31";
        }
    }

    public static Date getToday() {
        GregorianCalendar gcal = new GregorianCalendar();
        return gcal.getTime();
    }

    public static Date now() {
        return getToday();
    }

    public static String getCurTime() {
        return datetimeToString(getToday());
    }

    public static String getCurDate() {
        return dateToString(getToday());
    }

    public static String getChineseCurDate() {
        return dateToChineseString(getToday());
    }

    public static long hoursAfter(Date d1, Date d2) {
        long dif = 0L;
        dif = (d1.getTime() - d2.getTime()) / 3600000L;
        return dif;
    }

    public static long minutesAfter(Date d1, Date d2) {
        long dif = 0L;
        dif = (d1.getTime() - d2.getTime()) / 60000L;
        return dif;
    }

    public static long secondsAfter(Date d1, Date d2) {
        long dif = 0L;
        dif = (d1.getTime() - d2.getTime()) / 1000L;
        return dif;
    }

    public static int daysAfter(Date date1, Date date2) {
        long lndate1 = date1.getTime();
        long lndate2 = date2.getTime();
        int dif = (int) ((lndate1 - lndate2) / 1000L / 3600L / 24L);
        return dif;
    }

    public static int monthsBetween(Date beginDate, Date endDate) {
        GregorianCalendar gcBegin = new GregorianCalendar();
        GregorianCalendar gcEnd = new GregorianCalendar();
        if (beginDate.compareTo(endDate) > 0) {
            Date d = beginDate;
            beginDate = endDate;
            endDate = d;
        }

        gcBegin.setTime(beginDate);
        gcEnd.setTime(endDate);
        return (gcEnd.get(1) - gcBegin.get(1)) * 12 + gcEnd.get(2) - gcBegin.get(2);
    }

    public static Date relativeDate(Date baseDate, int n) {
        long ms = (long) (n * 24 * 60 * 60) * 1000L;
        return longToDatetime(baseDate.getTime() + ms);
    }

    public static Date relativeDate_M(Date baseDate, int n) {
        long ms = (long) n * 1000L;
        return longToDatetime(baseDate.getTime() + ms);
    }

    public static int getYear(Date date) {
        GregorianCalendar gcal = new GregorianCalendar();
        gcal.setTime(date);
        int year = gcal.get(1);
        return year;
    }

    public static int getQuarter(Date date) {
        GregorianCalendar gcal = new GregorianCalendar();
        gcal.setTime(date);
        int quarter = gcal.get(2) / 3 + 1;
        return quarter;
    }

    public static int getMonth(Date date) {
        GregorianCalendar gcal = new GregorianCalendar();
        gcal.setTime(date);
        int month = gcal.get(2) + 1;
        return month;
    }

    public static int getDay(Date date) {
        GregorianCalendar gcal = new GregorianCalendar();
        gcal.setTime(date);
        int day = gcal.get(5);
        return day;
    }

    public static int getHour(Date date) {
        Calendar cl = Calendar.getInstance();
        cl.setTime(date);
        int hour = cl.get(10);
        return hour;
    }

    public static int getHourOfDay(Date date) {
        Calendar cl = Calendar.getInstance();
        cl.setTime(date);
        int hour = cl.get(11);
        return hour;
    }

    public static int getMinute(Date date) {
        Calendar cl = Calendar.getInstance();
        cl.setTime(date);
        int minute = cl.get(12);
        return minute;
    }

    public static int getSecond(Date date) {
        Calendar cl = Calendar.getInstance();
        cl.setTime(date);
        int second = cl.get(13);
        return second;
    }

    public static int getDayOfWeek(Date date) {
        Calendar cl = Calendar.getInstance();
        cl.setTime(date);
        int weekDay = cl.get(7);
        return weekDay;
    }

    public static int getWeekOfMonth(Date date) {
        Calendar cl = Calendar.getInstance();
        cl.setTime(date);
        int weekDay = cl.get(4);
        return weekDay;
    }

    public static Date getFirstDayOfQuarter(Date date) {
        GregorianCalendar gcal = new GregorianCalendar();
        gcal.setTime(date);
        int quarter = getQuarter(date);
        int month = gcal.get(2);
        switch (quarter) {
            case 1:
                month = 0;
                break;
            case 2:
                month = 3;
                break;
            case 3:
                month = 6;
                break;
            case 4:
                month = 9;
        }

        gcal.set(gcal.get(1), month, 1, gcal.get(10), gcal.get(12), gcal.get(13));
        return gcal.getTime();
    }

    public static Date getEndDayOfQuarter(Date date) {
        GregorianCalendar gcal = new GregorianCalendar();
        gcal.setTime(date);
        int quarter = getQuarter(date);
        int month = gcal.get(2);
        switch (quarter) {
            case 1:
                month = 2;
                break;
            case 2:
                month = 5;
                break;
            case 3:
                month = 8;
                break;
            case 4:
                month = 11;
        }

        gcal.set(gcal.get(1), month, 1);
        return stringToDate(getEndDayOfMonth(gcal.getTime()));
    }

    public static Date addQuarter(Date date, int difQuarter) {
        return addMonth(date, 3 * difQuarter);
    }

    public static boolean checkDateFormat(String dateStr, String format) {
        if (dateStr != null && !"".equals(dateStr.trim())) {
            if (format == null || "".equals(format.trim())) {
                format = "yyyy-MM-dd";
            }

            if (format.length() != dateStr.length()) {
                return false;
            } else {
                SimpleDateFormat sdf = new SimpleDateFormat(format);

                try {
                    sdf.parse(dateStr);
                    return true;
                } catch (ParseException var4) {
                    return false;
                }
            }
        } else {
            return true;
        }
    }

    public static boolean checkDateFormatAndValue(String dateStr, String format) {
        boolean ret = true;
        if (dateStr != null && !"".equals(dateStr.trim())) {
            if (format == null || "".equals(format.trim())) {
                format = "yyyy-MM-dd";
            }

            if (format.length() != dateStr.length()) {
                return false;
            } else {
                String d_month = dateStr.substring(5, 7);
                SimpleDateFormat sdf = new SimpleDateFormat(format);
                Date d = null;

                try {
                    d = sdf.parse(dateStr);
                    int month = getMonth(d);
                    if (Integer.parseInt(d_month) != month) {
                        ret = false;
                    }
                } catch (ParseException var7) {
                    ret = false;
                }

                return ret;
            }
        } else {
            return true;
        }
    }

    public static String formatWeek(Date value, String formatStr) {
        if (value == null) {
            return null;
        } else {
            if (formatStr == null || "".equals(formatStr.trim())) {
                formatStr = "星期F";
            }

            SimpleDateFormat sf = new SimpleDateFormat(formatStr);
            return sf.format(value);
        }
    }

    public static int getDateInterval(Date dateBefore, Date dateAfter, int field, int amount) {
        int interval = 0;
        Calendar cb = new GregorianCalendar();
        Calendar da = new GregorianCalendar();
        cb.setTime(dateBefore);
        da.setTime(dateAfter);
        da.add(field, 1);
        GregorianCalendar c = (GregorianCalendar) cb;

        while (c.before(da)) {
            ++interval;
            c.add(field, amount);
        }

        return interval;
    }

    public static int getDateInterval(String beginDate, String endDate) {
        try {
            return getDateInterval(stringToDate(beginDate), stringToDate(endDate), 5, 1);
        } catch (Exception var3) {
            return 0;
        }
    }

    public static int getMonthInterval(Date dateBefore, Date dateAfter) {
        return getDateInterval(dateBefore, dateAfter, 2, 1);
    }

    public static String format(Date date, String pattern) {
        SimpleDateFormat formatter = new SimpleDateFormat(pattern);
        return formatter.format(date);
    }

    public static int compare2StrDate(String str1, String str2) {
        int compareResult = 0;
        if (str1 != null && str2 != null) {
            if (str1.length() == 10 && str2.length() == 10) {
                char[] char_str1 = str1.toCharArray();
                char[] char_str2 = str2.toCharArray();

                for (int i = 0; i < 10; ++i) {
                    char tmpChar1 = char_str1[i];
                    char tmpChar2 = char_str2[i];
                    if (tmpChar1 > tmpChar2) {
                        return 1;
                    }

                    if (tmpChar1 < tmpChar2) {
                        return 2;
                    }
                }

                return compareResult;
            } else {
                return 3;
            }
        } else {
            return 3;
        }
    }

    public static Date addMinute(Date date, int minute) {
        Date obj = new Date(date.getTime() + (long) (minute * 60 * 1000));
        return obj;
    }

    public static Date addSeconds(Date date, int minute) {
        Date obj = new Date(date.getTime() + (long) (minute * 1000));
        return obj;
    }

    public static int getWeekOfYear(Date date) {
        Calendar c = new GregorianCalendar();
        c.setFirstDayOfWeek(2);
        c.setMinimalDaysInFirstWeek(7);
        c.setTime(date);
        return c.get(3);
    }

    public static int getMaxWeekNumOfYear(int year) {
        Calendar c = new GregorianCalendar();
        c.set(year, 11, 31, 23, 59, 59);
        return getWeekOfYear(c.getTime());
    }

    public static Date getFirstDayOfWeek(int year, int week) {
        Calendar c = new GregorianCalendar();
        c.set(1, year);
        c.set(2, 0);
        c.set(5, 1);
        Calendar cal = (GregorianCalendar) c.clone();
        cal.add(5, week * 7);
        return getFirstDayOfWeek(cal.getTime());
    }

    public static Date getLastDayOfWeek(int year, int week) {
        Calendar c = new GregorianCalendar();
        c.set(1, year);
        c.set(2, 0);
        c.set(5, 1);
        Calendar cal = (GregorianCalendar) c.clone();
        cal.add(5, week * 7);
        return getLastDayOfWeek(cal.getTime());
    }

    public static Date getFirstDayOfWeek(Date date) {
        Calendar c = new GregorianCalendar();
        c.setFirstDayOfWeek(2);
        c.setTime(date);
        c.set(7, c.getFirstDayOfWeek());
        return c.getTime();
    }

    public static Date getLastDayOfWeek(Date date) {
        Calendar c = new GregorianCalendar();
        c.setFirstDayOfWeek(2);
        c.setTime(date);
        c.set(7, c.getFirstDayOfWeek() + 6);
        return c.getTime();
    }

    public static void main(String[] args) {
        System.out.println(getDayOfWeek(stringToDate("2017-04-29")));
    }
}
